"""
用于摄像机固定的场合，算法可以在一段时间学习场景后，识别场景中的运动物体
当运动物体停止运动后，过一段时间算法就把它作为场景的一部分了。
"""

import cv2
import numpy as np

bs=cv2.createBackgroundSubtractorKNN(detectShadows=True)
camera=cv2.VideoCapture(0)
while True:
    ret,frame =camera.read()
    fgmask=bs.apply(frame)  #获得前景的mask，前景用白色表示，gray for shadows
    th = cv2.threshold(fgmask.copy(),244,255,cv2.THRESH_BINARY)[1]
    dilated=cv2.dilate(th,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                                    (3,3)),iterations=2)
    image,contours,hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    for c in contours:
        if cv2.contourArea(c)>1600:
            (x,y,w,h)=cv2.boundingRect(c)
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,255,0),2)
    cv2.imshow("mog",fgmask)
    cv2.imshow("thresh",th)
    cv2.imshow("detection",frame)
    if cv2.waitKey(30) & 0xff==ord("q"):
        break
camera.release()
cv2.destroyAllWindows()